public partial class MonthSche : Form
    {
        public MonthSche()
        {
            InitializeComponent();
        }

        private void MonthSche_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'stafScheDataSet.Schedule' table. You can move, or remove it, as needed.
            this.scheduleTableAdapter.Fill(this.stafScheDataSet.Schedule);
            // TODO: This line of code loads data into the 'stafScheDataSet.Staff' table. You can move, or remove it, as needed.
            this.staffTableAdapter.Fill(this.stafScheDataSet.Staff);
            txtYear.Text = DateTime.Now.Year.ToString();
            cbMonth.Text = DateTime.Now.Month.ToString();
            Disp(txtYear.Text, cbMonth.Text);
        }

        void Disp(string year, string month)
        {
            dataGridView1.DataSource = null;
            DateTime dt0 = DateTime.Parse(year + "-" + month + "-1");
            DateTime dt1 = dt0.AddMonths(1);
            int iDays = (int)(dt1 - dt0).TotalDays;
            DataTable dtbl = new DataTable();
            dtbl.Columns.Add("StaffID");
            for (int i = 1; i <= iDays; i++)
            {
                dtbl.Columns.Add("D" + i.ToString("00"));
            }
            dtbl.Columns.Add("Total");
            try
            {
                DataTable dtSche = stafScheDataSet.Schedule.Select("startTime>='"
                    + dt0.ToString("yyyy-MM-dd") + "' and startTime<'"
                    + dt1.ToString("yyyy-MM-dd")
                    + "'", "StaffID").CopyToDataTable();
                DataRow drT = dtSche.Rows[0];
                string strTmp = drT["StaffID"].ToString();
                DateTime dtStart = DateTime.Parse(drT["StartTime"].ToString());
                string strDay = dtStart.Day.ToString("00");
                decimal dHours = decimal.Parse(drT["Hours"].ToString());
                decimal dTotalHours = dHours;
                DataRow dr1 = dtbl.NewRow();
                //foreach (DataColumn dc in dtbl.Columns)
                //{
                //    dr1[dc] = "None";
                //}
                dr1["StaffID"] = strTmp;
                dr1["D" + strDay] = dtStart.ToString("HH:mm")
                    + Environment.NewLine 
                    + dtStart.AddHours((double)dHours).ToString("HH:mm");
                dr1["Total"] = dHours;
                dtbl.Rows.Add(dr1);
                DataRow dtblCurRow = dr1;
                for (int i = 1; i < dtSche.Rows.Count; i++)
                {
                    drT = dtSche.Rows[i];
                    string strStaffID = drT["StaffID"].ToString();
                    dtStart = DateTime.Parse(drT["StartTime"].ToString());
                    strDay = dtStart.Day.ToString("00");
                    dHours = decimal.Parse(drT["Hours"].ToString());
                    if (strStaffID != strTmp)
                    {
                        dr1 = dtbl.NewRow();
                        //foreach (DataColumn dc in dtbl.Columns)
                        //{
                        //    dr1[dc] = "None";
                        //}
                        dr1["StaffID"] = strStaffID;
                        dr1["D" + strDay] = dtStart.ToString("HH:mm")
                            + Environment.NewLine 
                            + dtStart.AddHours((double)dHours).ToString("HH:mm");
                        dr1["Total"] = dHours;
                        dtbl.Rows.Add(dr1);
                        strTmp = strStaffID;
                        dtblCurRow = dr1;
                    }
                    else
                    {
                        dtblCurRow["D" + strDay] = dtStart.ToString("HH:mm")
                            + Environment.NewLine
                            + dtStart.AddHours((double)dHours).ToString("HH:mm");
                        dtblCurRow["Total"] = decimal.Parse(dtblCurRow["Total"].ToString())
                            + dHours;
                        dtbl.AcceptChanges();
                    }
                }
            }
            catch //(Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }
            if (dtbl.Rows.Count > 0)
            {
                dataGridView1.DataSource = dtbl;
                dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                for (int i = 1; i < dtbl.Columns.Count - 1; i++)
                {
                    dataGridView1.Columns[i].Width = 40;
                }
                dataGridView1.Columns[dtbl.Columns.Count - 1].AutoSizeMode
                    = DataGridViewAutoSizeColumnMode.AllCells;
            }
        }

        private void cbMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            Disp(txtYear.Text, cbMonth.Text);
        }
    }
